import { Injectable } from '@angular/core';
import {RequestUtil} from "../../../../util/request-util";
import {FormUtil} from "../../../../util/form-util";
import {Observable} from "rxjs";
import {MsgResult} from "../../../../models/msg-result";
import {Rule} from "./rule";
import {MsgAPI} from '../../msg-constants';

@Injectable()
export class RuleService {

  constructor(
    private request: RequestUtil,
    private formUtil: FormUtil
  ) { }

  //添加实例
  addRule(rule: Rule): Observable<MsgResult> {
    let params = "ruleName="+rule.ruleName;
    params += "&value="+encodeURI(encodeURI(rule.value));
    params += "&operation="+ encodeURI(encodeURI(rule.operation));
    params += "&isUse="+rule.isUse;
    return this.request.post(MsgAPI.insertRule, params);
  }

  //删除实例
  deleteRule(id: string): Observable<MsgResult> {
    return this.request.delete(MsgAPI.deleteRule + "&id=" + id);
  }

  //批量删除
  deleteRules(ids: Array<String>): Observable<MsgResult> {
    let params = "idStrs=" + ids;
    return this.request.post(MsgAPI.delRules, params);
  }

  //根据id获得规则
  getRuleById(id: string): Observable<MsgResult> {
    return this.request.get(MsgAPI.getRuleById + "&id=" + id);
  }

  //修改实例
  updateRule(rule: Rule): Observable<MsgResult> {
    let params = "ruleName="+rule.ruleName;
    params += "&id="+rule.id;
    params += "&value="+encodeURI(encodeURI(rule.value));
    params += "&operation="+ encodeURI(encodeURI(rule.operation));
    params += "&isUse="+rule.isUse;
    return this.request.put(MsgAPI.updateRule, params);
  }

  //无参获取所有记录
  getRule(): Observable<MsgResult> {
    return this.request.get(MsgAPI.getRule);
  }

}
